/**
 * @author Fayçal INAJJARANE
 */
package com.appspot.finajjarane.framework.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.appspot.finajjarane.framework.dao.IToolDao;
import com.appspot.finajjarane.framework.entities.MapDescription;
import com.appspot.finajjarane.framework.entities.Tool;
import com.appspot.finajjarane.framework.generic.ApplicationConstants;
import com.appspot.finajjarane.framework.generic.Utils;
import com.appspot.finajjarane.framework.models.ToolModel;
import com.appspot.finajjarane.framework.models.ToolModelForDao;
import com.appspot.finajjarane.framework.service.IToolService;
import com.google.appengine.api.datastore.Category;

@Service
public class ToolServiceImpl implements IToolService {

	@Autowired
	IToolDao toolDao;

	@Override
	public boolean toolCreateNew(ToolModelForDao toolModel) throws Exception {

		Tool tool = ToolModelForDaoToTool(toolModel);
		return toolDao.add(tool);

	}

	@Override
	public boolean toolUpdate(ToolModelForDao toolModel) throws Exception {
		Tool tool = ToolModelForDaoToTool(toolModel);
		try {
			toolDao.merge(tool);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	@Override
	public boolean toolRemove(Long toolId) throws Exception {
		return this.toolDao.removeByKey(toolId);
	}

	@Override
	public List<ToolModel> getToolsList(final int page, final int maxPerPage, final String lang)  throws Exception{

		List<ToolModel> toolsToReturn = new ArrayList<ToolModel>();
		List<Tool> tools = this.toolDao.getLimitedList(page, maxPerPage, lang, "publishedDate", "DESC");

		if(null==tools){
			return toolsToReturn;
		}

		for(Tool tool : tools){
			toolsToReturn.add(ToolToToolModel(tool));
		}

		return toolsToReturn;
	}

	@Override
	public ToolModel getTool(Long id)  throws Exception{

		Tool tool = this.toolDao.findByKey(id);
		return ToolToToolModel(tool);

	}

	@Override
	public List<ToolModel> getToolsAll()  throws Exception{
		List<Tool> tools = this.toolDao.findAll();
		List<ToolModel> toolModels = new ArrayList<ToolModel>();

		for(Tool tool : tools){
			toolModels.add(ToolToToolModel(tool));
		}

		return toolModels;
	}



	//###################### UTILITY METHODS #######################

	/**
	 * Utility method to convert Tool to ToolModel
	 * @param tool
	 * @return ToolModel
	 */
	private ToolModel ToolToToolModel(Tool tool){

		ToolModel toolModel = new ToolModel();
		List<String> tagsModels = new ArrayList<String>();

		for(Category tag : tool.getTags()){
			tagsModels.add(tag.getCategory());
		}

		toolModel.setId(tool.getId());
		toolModel.setTitle(tool.getTitle());
		for (MapDescription description : tool.getDescription()) {
			if(description.getLang() == Utils.getLanguage()){
				toolModel.setBody(description.getText().getValue());
				break;
			}
		}
		toolModel.setTags(tagsModels);
		toolModel.setImage(tool.getImage());
		toolModel.setPublishedDate(Utils.dateToDateDetailsModel(tool.getPublishedDate(),Utils.getLanguage() ));

		return toolModel;

	}


	/**
	 * Utility method to convert ToolModel to Tool
	 * @param toolModelForDao
	 * @return Tool
	 */
	private Tool ToolModelForDaoToTool(ToolModelForDao toolModelForDao){

		Tool tool = new Tool();

		List<Category> tags = new ArrayList<Category>();
		List<MapDescription> description = new ArrayList<MapDescription>();

		MapDescription descriptionAr = new MapDescription();
		MapDescription descriptionEn = new MapDescription();
		MapDescription descriptionFr = new MapDescription();

		descriptionAr.setLang(ApplicationConstants.LANG_AR_AR);
		descriptionAr.setText(toolModelForDao.getDescription().get(ApplicationConstants.LANG_AR_AR));

		descriptionEn.setLang(ApplicationConstants.LANG_EN_US);
		descriptionEn.setText(toolModelForDao.getDescription().get(ApplicationConstants.LANG_EN_US));

		descriptionFr.setLang(ApplicationConstants.LANG_FR_FR);
		descriptionFr.setText(toolModelForDao.getDescription().get(ApplicationConstants.LANG_FR_FR));

		for(String tag : toolModelForDao.getTags()){
			tags.add(new Category(tag));
		}

		tool.setId(toolModelForDao.getId());
		tool.setTitle(toolModelForDao.getTitle());
		tool.setTags(tags);
		tool.setDescription(description);
		tool.setPublishedDate(new Date());
		tool.setImage(toolModelForDao.getImage());


		return tool;

	}

	@Override
	public int getToolsCount(final String lang)  throws Exception{
		return this.toolDao.getEntriesCount("id",lang);
	}

}